package com.erp.Struts.Action;

import com.erp.Struts.common.Constants;
import com.erp.hibernate.beans.Employee;
import com.erp.hibernate.beans.Role;
import com.erp.hibernate.dao.EmployeeDAO;
import com.erp.hibernate.dao.RoleDAO;

public class PowerAction extends PageAction {
	private static final long serialVersionUID = -8282814038189152107L;
	private EmployeeDAO employeeDAO;
	private RoleDAO roleDAO;
	private Employee employee;
	private String roleid;
	private String uid;
	
	public String init() throws Exception{
		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getEmployeeDAO().findPager(super.pageSize, super.pageNo);
		setSession(Constants.PAGER_POWER, super.pager);
		return Constants.LIST_KEY;
	}
	
	public String list() throws Exception{
		super.pager = this.getEmployeeDAO().findPager(super.pageSize, super.pageNo);
		setSession(Constants.PAGER_POWER, super.pager);
		return Constants.LIST_KEY;
	}
	
	public String add() throws Exception{
		reset();
		return Constants.ADD_KEY;
	}
	
	public String insert() throws Exception{
		Role role = this.getRoleDAO().findById(Integer.valueOf(roleid));
		employee.setRole(role);
		this.getEmployeeDAO().save(employee);

		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getEmployeeDAO().findPager(super.pageSize, super.pageNo);
		setSession(Constants.PAGER_POWER, super.pager);
		return Constants.LIST_KEY;
	}
	
	public String edit() throws Exception{
		if (uid == null) {
			return Constants.LIST_KEY;
		} else {
			Employee em2 = this.getEmployeeDAO().findById(Integer.valueOf(uid));

			if (em2 == null) {
				return Constants.LIST_KEY;
			} else {
				employee = em2;
				return Constants.EDIT_KEY;
			}
		}
	}
	
	public String del() throws Exception{
		Employee em = this.getEmployeeDAO().findById(Integer.valueOf(uid));
		this.getEmployeeDAO().delete(em);
		
		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getEmployeeDAO().findPager(super.pageSize, super.pageNo);
		return Constants.LIST_KEY;
	}
	
	public String change() throws Exception{
		Employee em = this.getEmployeeDAO().findById(Integer.valueOf(uid));
		Role role = this.getRoleDAO().findById(Integer.valueOf(roleid));
		em.setEnabled(employee.getEnabled());
		em.setName(employee.getName());
		em.setPassword(employee.getPassword());
		em.setRole(role);
		this.getEmployeeDAO().update(em);
		
		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getEmployeeDAO().findPager(super.pageSize, super.pageNo);
		return Constants.LIST_KEY;
	}
	
	public String setdefault() throws Exception{
		Employee em = this.getEmployeeDAO().findById(Integer.valueOf(uid));
		em.setPassword("111111");
		this.getEmployeeDAO().update(em);
		
		super.pageSize = Constants.pageSize;
		super.pageNo = Constants.pageNo;
		super.pager = this.getEmployeeDAO().findPager(super.pageSize, super.pageNo);
		return Constants.LIST_KEY;
	}
	
	private void reset() {
		employee = null;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public RoleDAO getRoleDAO() {
		return roleDAO;
	}

	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	public String getRoleid() {
		return roleid;
	}

	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}
}
